#ifndef PONG_LEVEL_H
#define PONG_LEVEL_H

#include <OgreMeshManager.h>
#include <OgreSceneNode.h>
#include <OgreSceneManager.h>
#include <OgreAxisAlignedBox.h>
#include <OgreEntity.h>
#include <OgrePlane.h>

//#include "AxisAlignedBoundingVolume.h"

class Level {
protected:
    Ogre::AxisAlignedBox bounds;

public:
    Level(const Ogre::AxisAlignedBox& levelBounds) 
        : bounds(levelBounds)
    {
    }

    const Ogre::AxisAlignedBox& boundaries(void) const {return bounds;}

    void render(Ogre::SceneNode* const node) const {
        using namespace Ogre;
        
        SceneManager* sceneMan = node->getCreator();
        MeshManager* meshMan = MeshManager::getSingletonPtr();
        /*const char* const names[6] = {"levelBound+x", "levelBound-x", "levelBound+y", "levelBound-y", "levelBound+z", "levelBound-z"};
        const Vector3 pos[6] = {bounds.getMaximum(), &AxisAlignedBox::getMinimum};
        for (size_t i = 0; i < 6; ++i) {*/
        meshMan->createPlane("levelbound_x", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
                             Plane(1, 0, 0, 0), bounds.getSize().z, bounds.getSize().y);
        meshMan->createPlane("levelbound_y", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
                             Plane(0, 1, 0, 0), bounds.getSize().x, bounds.getSize().z, 
                             1, 1, true, 1u, 1, 1, Vector3::NEGATIVE_UNIT_Z);
        meshMan->createPlane("levelbound_z", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, 
                             Plane(0, 0, 1, 0), bounds.getSize().x, bounds.getSize().y);

        Entity* planeGfx = sceneMan->createEntity("levelbound_x");
        SceneNode* n = node->createChildSceneNode(Vector3(bounds.getMinimum().x, 0, 0));
        n->attachObject(planeGfx);
        planeGfx = sceneMan->createEntity("levelbound_x");
        n = node->createChildSceneNode(Vector3(bounds.getMaximum().x, 0, 0), Quaternion(0, 0, 1, 0));
        n->attachObject(planeGfx);
        planeGfx = sceneMan->createEntity("levelbound_y");
        n = node->createChildSceneNode(Vector3(0, bounds.getMinimum().y, 0));
        n->attachObject(planeGfx);
        planeGfx = sceneMan->createEntity("levelbound_y");
        n = node->createChildSceneNode(Vector3(0, bounds.getMaximum().y, 0), Quaternion(0, -1, 0, 0));
        n->attachObject(planeGfx);
        planeGfx = sceneMan->createEntity("levelbound_z");
        n = node->createChildSceneNode(Vector3(0, 0, bounds.getMinimum().z));
        n->attachObject(planeGfx);
        planeGfx = sceneMan->createEntity("levelbound_z");
        n = node->createChildSceneNode(Vector3(0, 0, bounds.getMaximum().z), Quaternion(0, -1, 0, 0));
        n->attachObject(planeGfx);
        node->showBoundingBox(true);
    }
};
#endif //PONG_LEVEL_H
